package com.ruoqing.inform.service.message;

import com.ruoqing.core.constant.Constants;
import com.ruoqing.core.dto.admin.AdminMessageDto;
import com.ruoqing.inform.constant.qqmail.QqEmailConstantEnum;
import com.ruoqing.inform.service.api.MailMessageService;
import com.ruoqing.inform.service.api.PhoneCodeService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
	* @author YaoXian
	* @title: SmsServiceImpl
	* @projectName ruoxi-blog
	* @description: 若兮微服务 - 通知服务 - 后台管理中心模块 - 异步接收队列消息发送通知
	* @date 2020-11-28 21:27
	*/
@Slf4j
@Service
@RocketMQMessageListener(consumerGroup = "ruoqing-inform-msgConsumer-group", topic = "admin_topic")
public class AdminSmsServiceImpl implements RocketMQListener<AdminMessageDto> {
		
		@Resource
		private PhoneCodeService phoneCodeService;
		
		@Resource
		private MailMessageService mailMessageService;
		
		/**
			* 订阅消息队列中可靠异步消息
			* 根据条件异步解耦执行方法
			* @param adminMessageDto
			*/
		@SneakyThrows
		@Override
		public void onMessage(AdminMessageDto adminMessageDto) {
				if (Constants.responseOk.equals(adminMessageDto.getUserResponse().getStatus())){
						switch (adminMessageDto.getModule()){
								case deleteUser:
										log.info("通知服务收到 " + adminMessageDto.getUserResponse().getMessage() +" 即将执行发送通知邮件服务!");
										mailMessageService.sendMailMessage(
														QqEmailConstantEnum.sender.getContent(),
														adminMessageDto.getUserResponse().getUserEntity().getEmail(),
														QqEmailConstantEnum.emailTitle.getContent(),
														"鉴于您在若兮博客的违规行为,您名为 "+adminMessageDto.getUserResponse().getUserEntity().getUsername()+
																		" 的账号" + "已经被管理员从若兮博客会员列表中移除," +
																		"并添加至黑名单," + "与该账号绑定为 "+adminMessageDto.getUserResponse().getUserEntity().getPhoneNumber()+
																		" 的手机号将永远无法在若兮博客中注册!");
										break;
								case updateUser:
										log.info("通知服务收到 " + adminMessageDto.getUserResponse().getMessage()+" 即将执行发送通知邮件服务!");
										mailMessageService.sendMailMessage(
														QqEmailConstantEnum.sender.getContent(),
														adminMessageDto.getUserResponse().getUserEntity().getEmail(),
														QqEmailConstantEnum.emailTitle.getContent(),
														"若兮管理员修改了您在若兮博客的用户信息,修改理由: "+adminMessageDto.getParam()
																		+"注: 除若兮超级管理员外,任何人无法修改用户登陆密码!");
										break;
								case insertUser:
										log.info("通知服务收到 " + adminMessageDto.getUserResponse().getMessage()+" 即将执行发送通知邮件服务!");
										mailMessageService.sendMailMessage(
														QqEmailConstantEnum.sender.getContent(),
														adminMessageDto.getUserResponse().getUserEntity().getEmail(),
														QqEmailConstantEnum.emailTitle.getContent(),
														"恭喜您,管理员在若兮博客中创建用户成功!用户名为:"
										+adminMessageDto.getUserResponse().getUserEntity().getUsername());
										break;
								default:
										log.error("错误的后台管理通知参数类型!");
						}
						
				}else {
						log.error("状态码错误!无法发送通知!");
				}
		}
}
